package com.nbcio.modules.erp.refund.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.nbcio.modules.erp.refund.entity.ReturnRequest;
import com.nbcio.modules.erp.refund.service.IReturnRequestService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 退换货请求
 * @Author: nbacheng
 * @Date:   2024-04-28
 * @Version: V1.0
 */
@Api(tags="退换货请求")
@RestController
@RequestMapping("/refund/returnRequest")
@Slf4j
public class ReturnRequestController extends JeecgController<ReturnRequest, IReturnRequestService> {
	@Autowired
	private IReturnRequestService returnRequestService;
	
	/**
	 * 分页列表查询
	 *
	 * @param returnRequest
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "退换货请求-分页列表查询")
	@ApiOperation(value="退换货请求-分页列表查询", notes="退换货请求-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ReturnRequest returnRequest,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ReturnRequest> queryWrapper = QueryGenerator.initQueryWrapper(returnRequest, req.getParameterMap());
		Page<ReturnRequest> page = new Page<ReturnRequest>(pageNo, pageSize);
		IPage<ReturnRequest> pageList = returnRequestService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param returnRequest
	 * @return
	 */
	@AutoLog(value = "退换货请求-添加")
	@ApiOperation(value="退换货请求-添加", notes="退换货请求-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ReturnRequest returnRequest) {
		returnRequestService.save(returnRequest);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param returnRequest
	 * @return
	 */
	@AutoLog(value = "退换货请求-编辑")
	@ApiOperation(value="退换货请求-编辑", notes="退换货请求-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ReturnRequest returnRequest) {
		returnRequestService.updateById(returnRequest);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "退换货请求-通过id删除")
	@ApiOperation(value="退换货请求-通过id删除", notes="退换货请求-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		returnRequestService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "退换货请求-批量删除")
	@ApiOperation(value="退换货请求-批量删除", notes="退换货请求-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.returnRequestService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "退换货请求-通过id查询")
	@ApiOperation(value="退换货请求-通过id查询", notes="退换货请求-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ReturnRequest returnRequest = returnRequestService.getById(id);
		if(returnRequest==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(returnRequest);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param returnRequest
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ReturnRequest returnRequest) {
        return super.exportXls(request, returnRequest, ReturnRequest.class, "退换货请求");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ReturnRequest.class);
    }


	 @AutoLog(value = "退换货请求-新增退换货请求（商城）")
	 @ApiOperation(value="退换货请求-新增退换货请求（商城）", notes="退换货请求-新增退换货请求（商城）")
	 @PostMapping(value = "/addReturnRequest")
	 public Result<?> addRefundRequest(@RequestBody List<Map<String,Object>> map) {
		 returnRequestService.addReturnRequest(map);
		 return Result.OK("退换货请求成功");
	 }

	 @AutoLog(value = "退换货请求-批量审核")
	 @ApiOperation(value="退换货请求-批量审核", notes="退换货请求-批量审核")
	 @PostMapping(value = "/batchCommit")
	 public Result<?> batchCommit(@RequestBody List<String> ids) {
		 returnRequestService.batchCommit(ids);
		 return Result.OK("批量审核成功");
	 }

	 @AutoLog(value = "退换货请求-批量拒绝")
	 @ApiOperation(value="退换货请求-批量拒绝", notes="退换货请求-批量拒绝")
	 @PostMapping(value = "/batchRefuse")
	 public Result<?> batchRefuse(@RequestBody List<String> ids) {
		 returnRequestService.batchRefuse(ids);
		 return Result.OK("批量拒绝成功");
	 }

	 @AutoLog(value = "退换货请求-审核")
	 @ApiOperation(value="退换货请求-审核", notes="退换货请求-审核")
	 @GetMapping(value = "/commit")
	 public Result<?> commit(@RequestParam("id") String id) {
		 returnRequestService.commit(id);
		 return Result.OK("审核成功");
	 }

	 @AutoLog(value = "退换货请求-拒绝")
	 @ApiOperation(value="退换货请求-拒绝", notes="退换货请求-拒绝")
	 @GetMapping(value = "/refuse")
	 public Result<?> refuse(@RequestParam("id") String id) {
		 returnRequestService.refuse(id);
		 return Result.OK("拒绝成功");
	 }
}
